﻿namespace MicroCloud.Identity.Entities
{
    /// <summary>
    /// 用户声明基类
    /// </summary>
    /// <typeparam name="TKey">用户声明编号类型</typeparam>
    /// <typeparam name="TUserKey">用户编号类型</typeparam>
    public abstract class UserClaimBase<TKey, TUserKey> : EntityBase<TKey>, ICreatedTime
        where TKey : struct, IEquatable<TKey>
        where TUserKey : struct, IEquatable<TUserKey>
    {
        #region "属性"
        #region "获取或设置 用户编号"
        /// <summary>
        /// 获取或设置 用户编号
        /// </summary>
        [Column("UserId")]
        [Required]
        [DisplayName("用户编号")]
        public TUserKey UserId { get; set; }
        #endregion
        #region "获取或设置 声明类型"
        private string _ClaimType = "";
        /// <summary>
        /// 获取或设置 声明类型
        /// </summary>
        [Column("ClaimType")]
        [Required, DefaultValue("")]
        [StringSize(StringSizeType.Name)]
        [DisplayName("声明类型")]
        public string ClaimType { get { return _ClaimType; } set { _ClaimType = value.ReplaceNull(); } }
        #endregion
        #region "获取或设置 声明值"
        private string _ClaimValue = "";
        /// <summary>
        /// 获取或设置 声明值
        /// </summary>
        [Column("ClaimValue")]
        [StringSize(StringSizeType.General)]
        [DisplayName("声明值")]
        public string ClaimValue { get { return _ClaimValue; } set { _ClaimValue = value.ReplaceNull(); } }
        #endregion

        #region "获取或设置 创建时间 ICreatedTime、ICreationAudited  可装载接口，在数据创建时可自动设置当前时间"
        /// <summary>
        /// 获取或设置 创建时间
        /// </summary>
        [Column("CreatedTime")]
        [DisplayName("创建时间")]
        public DateTime CreatedTime { get; set; }
        #endregion

        #endregion

        #region "方法"
        #region "使用类型和值创建一个声明对象"
        /// <summary>
        /// 使用类型和值创建一个声明对象
        /// </summary>
        /// <returns></returns>
        public virtual Claim ToClaim()
        {
            return new Claim(ClaimType, ClaimValue);
        }
        #endregion
        #region "使用一个声明对象初始化"
        /// <summary>
        /// 使用一个声明对象初始化
        /// </summary>
        /// <param name="other">声明对象</param>
        public virtual void InitializeFromClaim(Claim other)
        {
            ClaimType = other?.Type;
            ClaimValue = other?.Value;
        }
        #endregion

        #endregion

    }
}
